<?php

//------------------------------------------------------------------------- Connect to database to $db
if($config["database"]=="mysql"){
	try{
		$db = new PDO("mysql:host=".$config["databaseserver"].";port=".$config["databaseserverport"].";dbname=".$config["databaseserverdbname"]."", $config["databaseserveruser"], $config["databaseserverpassword"], array( PDO::ATTR_PERSISTENT => true));
	}catch( PDOException $exception ){
		die("DB Link : ".$exception->getMessage());
	}
}
elseif($config["database"]=="sqlite3"){
	try{
		$db = new PDO('sqlite:'.$config["basefolder"].'db/xAP.db3');
	}catch( PDOException $exception ){
		die($exception->getMessage());
	}
}
else  //if unknown use sqlite
{
	if ($db = sqlite_popen($config["basefolder"].'db/xAP.db', 0666, $sqliteerror)) { 

	} else {
	    die($sqliteerror);
	}
}


function dbquery($sql){
	global $config,$db;
	if($config["database"]=="mysql"){
		try{
			$statement = $db->query($sql);	
		}catch( PDOException $exception ){
			xAPLog("SQL","ERROR","$sql"); 	
		}
		if ($statement!=False){
			$results = @$statement->fetchAll(PDO::FETCH_ASSOC);
		}
		else
		{
			xAPLog("SQL","Result ERROR","$sql"); 	
		}
	}
	elseif($config["database"]=="sqlite3"){
		try{
			$statement = $db->query($sql);	
		}catch( PDOException $exception ){
			xAPLog("SQL","ERROR","$sql"); 	
		}
		if ($statement!=False){
			$results = @$statement->fetchAll(PDO::FETCH_ASSOC);
		}
		else
		{
			xAPLog("SQL","Result ERROR","$sql"); 	
		}
	}
	else  //if unknown use sqlite
	{
		try{
			$results=sqlite_query($db, $sql);
		}catch(Exception $e){
			xAPLog("SQL","ERROR","$sql");
		}
	}	
	return $results;
}

function dbexec($sql){
	global $config,$db;
	if($config["database"]=="mysql"){
		$db->exec($sql);
	}
	elseif($config["database"]=="sqlite3"){
		$db->exec($sql);
	}
	else  //if unknown use sqlite
	{
		$results=sqlite_query($db, $sql);
	}	
	return $results;
}

function dbesc($data){
	// escape data
	if($config["database"]=="mysql"){
		$data=mysql_escape_string($data);
	}
	elseif($config["database"]=="sqlite3"){
		$data=sqlite_escape_string($data);
	}
	else  //if unknown use sqlite
	{
		$data=sqlite_escape_string($data);
	}	
	return $data;
}

function dbfetcharray($data){
	// escape data
	if($config["database"]=="mysql"){

	}
	elseif($config["database"]=="sqlite3"){
		$results=sqlite_escape_string($data);
	}
	else  //if unknown use sqlite
	{
		$results=sqlite_escape_string($data);
	}	
	return $results;
}
function dbtransactionstart(){
	global $db;
	// escape data
	if($config["database"]=="mysql"){
		dbexec("BEGIN TRANSACTION;");
	}
	elseif($config["database"]=="sqlite3"){
		//$db->beginTransaction();
		dbexec("BEGIN TRANSACTION;");
	}
	else  //if unknown use sqlite
	{

	}	
	return $results;
}
function dbtransactionend(){
	global $db;
	// escape data
	if($config["database"]=="mysql"){
		dbexec("COMMIT TRANSACTION;");
	}
	elseif($config["database"]=="sqlite3"){
		//$db->commit();
		dbexec("COMMIT TRANSACTION;");
	}
	else  //if unknown use sqlite
	{

	}	
	return $results;
}
?>